<?php

namespace App\Admin\Controllers;

use App\Models\SPictureInfo;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class PictureController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('图片')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('图片')
            ->description('详情')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('图片')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('图片')
            ->description('新增')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new SPictureInfo);

        $grid->id('ID')->sortable();
        $grid->name('图片名称');
        $grid->pic_url('图片')->image();
        $grid->pic_type('图片分类')->display(function ($pic_type) {
            return config('svs.pic_type.'.$pic_type);
        });

        $grid->to_url('跳转地址');
        // $grid->course_type('图片课程类型');
        $grid->created_at('创建时间')->sortable();
        $grid->updated_at('更新时间');

        //禁用导出
        $grid->disableExport();

        //查询
        $grid->filter(function($filter){

            // 去掉默认的id过滤器
            // $filter->disableIdFilter();
            $filter->like('name', '图片名称');
            $filter->in('pic_type',  "图片分类")->multipleSelect(config("svs.pic_type"));

        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(SPictureInfo::findOrFail($id));

        $show->id('Id');
        $show->name('图片名称');
        $show->pic_type('图片分类')->as(function($pic_type) {
            return config('svs.pic_type.'.$pic_type);
        });
        $show->pic_url('图片')->image();
        $show->to_url('跳转地址');
        // $show->course_type('Course type');
        // $show->admin_id('上传人');
        $show->created_at('创建时间');
        $show->updated_at('更新时间');
        // $show->deleted_at('Deleted at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new SPictureInfo);
        $form->text("name", "图片名称");
        $form->select("pic_type", "图片分类")->options(config("svs.pic_type"))->rules('required')->default(0);

        $form->text("to_url", "跳转地址");


        $form->image("pic_url", "上传图片")->rules('required');

        $form->display('created_at', '创建时间');
        $form->display('updated_at', '更新时间');
        $form->hidden('admin_id')->value( \Admin::user()->id );

        $form->footer(function ($footer) {

            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();

            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();

        });
        return $form;
    }
}
